package com.shuyun.xxx.guava.offer;

import java.util.Stack;

/**
 * Created by goodb on 2017/5/21.
 */
public class GetMinTest {
    Stack<Integer> stackData;
    Stack<Integer> stackMin;
    public GetMinTest(){
        stackData = new Stack<>();
        stackMin = new Stack<>();
    }
    public void push(int data){
        stackData.push(data);
        if(stackMin.isEmpty()){
            stackMin.push(data);
        }else if(data <= this.getMin()){
            stackData.push(data);
        }
    }

    public int pop(){
        if(stackData.isEmpty()){
            throw new RuntimeException("stack is empty");
        }
        int value = stackData.pop();
        if(value == getMin()){
            stackMin.pop();
        }
        return value;
    }

    public int getMin(){
        if(stackMin.isEmpty()){
            throw new RuntimeException("stack is empty");
        }
        return stackMin.peek();
    }
}
